package com.sx.manage.service.report;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sx.common.util.json.JackJsonUtil;
import com.sx.manage.util.HttpUtil;
import com.sx.manage.util.PropertiesUtils;
import com.sx.model.constants.MSystemConstants;
import com.sx.model.manage.mybatis.vo.auth.MAutoVO;
import com.sx.model.manage.mybatis.vo.consumer.login.MConsumerLoginVO;

/**
 *  报表系统 web 接口实现类
 * @author Administrator
 *
 */
@Service("reposrWebServiceImpl")
public class MReposrWebServiceImpl {
	private Logger _log = LoggerFactory.getLogger(this.getClass());
	/** 获取加密系统参数配置**/
	public MAutoVO authConfig(){
		_log.debug("获取加密系统参数配置");
		String url = PropertiesUtils.getProperty(MSystemConstants.AUTH_SYSTEM_URL);
		String port = PropertiesUtils.getProperty(MSystemConstants.AUTH_SYSTEM_PORT);
		String name = PropertiesUtils.getProperty(MSystemConstants.AUTH_SYSTEM_NAME);
		String method = PropertiesUtils.getProperty(MSystemConstants.AUTH_SYSTEM_ENCRYPT_METHOD);
		

		if( StringUtils.isBlank( url )
				|| StringUtils.isBlank( name )
				|| StringUtils.isBlank( method )
				){
			_log.debug("加密系统参数配置异常 url="+url+",port="+port+",name="+name+",method="+method);
			return new MAutoVO(false,null,"加密系统参数配置异常");
		}
		StringBuilder url_str = new StringBuilder();
					 url_str.append(url);
					 if(StringUtils.isNotBlank( port )){
						 url_str.append(":")
					        	.append(port);
					 }
					 url_str.append("/")
					        .append(name)
					        .append("/")
					        .append(method);
		 _log.debug("访问加密系统:url_str="+url_str);				
		 MAutoVO autoVO = new MAutoVO();
		 		 autoVO.setUrl_str(url_str.toString());
		 return autoVO;
	}
	/** 获取报表系统参数配置**/
	public MAutoVO reportConfig(){
		_log.debug("获取报表系统参数配置");
		String url = PropertiesUtils.getProperty(MSystemConstants.REPORT_SYSTEM_URL);
		String port = PropertiesUtils.getProperty(MSystemConstants.REPORT_SYSTEM_PORT);
		String name = PropertiesUtils.getProperty(MSystemConstants.REPORT_SYSTEM_NAME);		

		if( StringUtils.isBlank( url )
				|| StringUtils.isBlank( name )
				){
			_log.debug("报表系统参数配置异常 url="+url+",port="+port+",name="+name);
			return new MAutoVO(false,null,"报表系统参数配置异常");
		}
		StringBuilder url_str = new StringBuilder();
					 url_str.append(url);
					 if(StringUtils.isNotBlank( port )){
						 url_str.append(":")
					        	.append(port);
					 }					       
					 url_str.append(name);
		 _log.debug("访问报表系统:url_str="+url_str);				
		 MAutoVO autoVO = new MAutoVO();
		 		 autoVO.setUrl_str(url_str.toString());
		 return autoVO;
	}
	/** 信息加密**/
	public MAutoVO encrypt( MConsumerLoginVO loginVO,String report_url ){
		_log.debug("加密方法开始执行");
		if( null == loginVO){
			_log.debug("用户没有登录或超时");
			return new MAutoVO(false,null,"用户没有登录或超时");
		}
		//加密系统参数
		MAutoVO autoVOAuth = this.authConfig();
		if( !autoVOAuth.isSuccess()){
			_log.debug("获取加密系统参数错误："+autoVOAuth.getMsg());
			return autoVOAuth;
		}
		//报表系统参数
		MAutoVO  autoVOreport = reportConfig();
		if( !autoVOreport.isSuccess()){
			_log.debug("获取报表系统参数错误："+autoVOreport.getMsg());
			return autoVOreport;
		}
		autoVOreport.setUrl_str(autoVOreport.getUrl_str()+report_url);
		MAutoVO autoVO = new MAutoVO();
				autoVO.setToken_name(MSystemConstants.REPORT_TOKEN_MANAGER);
				autoVO.setSystem_consumer_id( loginVO.getConsumer_id());			
		MAutoVO returnAutoVO = null;
		try{
			String result = HttpUtil.post( autoVOAuth.getUrl_str(), JackJsonUtil.objToJson(autoVO));
			_log.debug("接口调通返回结果result = "+result);	
			returnAutoVO=JackJsonUtil.jsonToJavaBean(result, MAutoVO.class);				
			StringBuilder url_str_ = new StringBuilder();
						 url_str_.append( autoVOreport.getUrl_str())
						 		 .append("?auth_str=")
						 		 .append(returnAutoVO.getAuth_str());
			returnAutoVO.setAuth_str( url_str_.toString() );
			_log.debug("访问报表系统url="+url_str_.toString());
		}catch(Exception ex){
			_log.error("调用报表系统出错：",ex);
			return new MAutoVO(false,null,"报表系统可能暂时关闭，请稍后再试");
		}						    
		return returnAutoVO;
	}
}
